package cn.thr.pethome.department.controller;

import cn.thr.pethome.department.domain.Department;
import cn.thr.pethome.department.domain.DepartmentRo;
import cn.thr.pethome.department.query.DepartmentQuery;
import cn.thr.pethome.department.service.IDepartmentService;
import cn.thr.pethome.util.AjaxResult;
import cn.thr.pethome.util.BusinessException;
import cn.thr.pethome.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/dept")
//@Api 标注这个controller 是用来干嘛的
@Api(value = "部门的Api",description = "部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService iDepartmentService;


    //@PostMapping 相当于 @RequestMapping这是用post 发请求因为是用来查询数据
    @PostMapping("/list")
    @ResponseBody
    // @ApiOperation 标注这个方法是用来干嘛的！
    @ApiOperation(value = "这是获取所有部门的信息")
    public List<Department> findAll(){
        return iDepartmentService.findAll();
    }


    //@GetMapping 查询一个发送get请求  "/{id}" 这个id只是一个占位符 等会这个id会传给Long id
    @GetMapping("/{id}")
    @ResponseBody
    @ApiOperation(value = "这个是用id来查询部门信息")
    //@PathVariable("id") 从path 路径中获取到的id 返回给Long id
    public AjaxResult findOne(@PathVariable("id") Long id){
        try {
            Department one = iDepartmentService.findOne(id);
            return AjaxResult.success().setData(one).setMsg("查找到一坨狗屎");
            //通过工具类返回信息是否成功或者失败  setData(one)是用来返回数据 setMsg修改要显示的文字
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("来嘛，哦豁了嘛");
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    @ApiOperation(value = "这是根据id删除数据")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            iDepartmentService.delete(id);
            return AjaxResult.success().setMsg("你猜下删除成功没有");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }

    }

    /**
     *
     * @RequestBody 是前端传进来
     * @ResponseBody 是后端返回给前端的
     *
     */
    @PutMapping
    @ResponseBody //前端传后端
    @ApiOperation(value = "如果有id的话就是修改，没有id的情况下就是添加")
    public AjaxResult save(@RequestBody Department department){
        try {
            if (department.getId()!=null) {
                iDepartmentService.update(department);
            }else {
                iDepartmentService.add(department);
            }
            return AjaxResult.success().setMsg("你猜成功没得");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }

    }


    /**
     *
     * @RequestBody 是前端传进来
     * @ResponseBody 是后端返回给前端的
     *
     */
    @PostMapping("/pageList")
    @ResponseBody
    @ApiOperation(value = "用来做分页查询的")
    public AjaxResult findQuery(@RequestBody DepartmentQuery query){
        try {
            //获取到当前页
            List<Department> byQuery = iDepartmentService.findByQuery(query);
            //这是获取到总条数
            Long count = iDepartmentService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
            //setData 用来返回数据的   setTotal 用来返回总条数
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @PatchMapping("/del")
    @ResponseBody
    @ApiOperation(value = "这是用来批量删除的")
    //@RequestBody 把后端都数据返回给前端
    public  AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            iDepartmentService.batchDel(ids);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败");
    }
    @GetMapping("/tree")
    @ApiOperation(value = "返回部门数")
    @ResponseBody
    public List<Department> getDeptTree(){
        return iDepartmentService.deptTree();
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response){
        List<Department> all = iDepartmentService.findAll();
        System.out.println(all);
        ExcelUtil.exportExcel(all,"部门数据ti","部门数据",Department.class,"导出的部门数据.xls",response);
    }
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Department> shops = ExcelUtil.importExcel(file, 1, 1, Department.class);
        //做数据校验
        shops.forEach(System.out::println);
    }

    @GetMapping("/countByDep")
    public AjaxResult countByDep(){
        try {
            List<DepartmentRo> countDept = iDepartmentService.getCountDept();
            AjaxResult s=AjaxResult.success().setData(countDept);
//            return AjaxResult.success().setData(countDept);
            return s;
        }catch (BusinessException businessException){
            return AjaxResult.error().setMsg(businessException.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return  AjaxResult.error().setMsg("电脑关机重试");
        }
    }
}